/**
******************************************************************************************
* @file ChangeLedByLed.c
* @brief Command for change leds one by one over the time.
* @date 12 mai 2015
* @author Benjamin Lancinot
*****************************************************************************************/
/* COPYRIGHT ****************************************************************************
*****************************************************************************************
* Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
*    list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************************/

#include "ChangeLedByLed.h"

/*******************************************************************************************
* PROTOYPES
********************************************************************************************/

void ChangeLedByLed_destroy(Command **);

void ChangeLedByLed_execute(Command *, char*);

/*******************************************************************************************
* FUNCTIONS
********************************************************************************************/

/**
 * @brief Constructor
 * @param ledshandyman Pointer to the object which can modify state of leds
 * @return Pointer to a new Command
 **/
Command* ChangeLedByLed_create(LedsHandyman * ledshandyman){
	Command* This = ( Command*) malloc(sizeof(*This));

        //Init functions
        This->execute = ChangeLedByLed_execute;
	This->destroy = ChangeLedByLed_destroy;
        //unused param
        This->valueToChange = ledshandyman;

	return This;
}


/**
 * @brief Destructor
 * @param This Double pointer to the wanted Command
 **/
void ChangeLedByLed_destroy(Command **This){
        if (*This) {
        	free(*This);
		*This = NULL;
	}
}


/**
 * @brief Ask change color of led one by one with a specific time
 * @param This Pointer to the wanted ChangeAllLeds
 * @param sequence Pointer to characters which permit to know the action to make
 **/
void ChangeLedByLed_execute(Command * This, char* sequence){

	if (strlen(sequence) <10)
	{
          	uint8_t seq[7];
		int i;
                LedsHandyman * ledshandyman = (LedsHandyman *) This->valueToChange;
           seq[0]=(uint8_t) (4);
	   for (i = 1; i < 7; i++) {
		   seq[i]=(uint8_t) (sequence[i-1]-48);
	   }
           ledshandyman->tinkerBulbs(ledshandyman, seq);
	}

}
